<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * class Page_lib
 * @author Davinder
 * @version 1.0
 */
 
class Page_lib
{
    private $_CI;
    private $_picture_config;
    
    public function __construct() {
        $this->_CI = & get_instance();
        log_message('ERROR',__CLASS__.' Class Initialized');
        $this->_CI->load->model('page_model');
        
        $this->_CI->config->load('scrapm');
        $scrapm_config = $this->_CI->config->item('scrapm');
        $this->picture_config = $scrapm_config['picture_config'];
	}
    
    public function get_page_details($p_data=null) {
        
        $this->_CI->load->helper('url');
        
        $pageDetails = $this->_CI->page_model->get_page_details($p_data);
        
        if ($pageDetails) {
            $pagePictures = $this->_CI->page_model->get_page_images($p_data);
            $pageDetails->pictures = (array) $pagePictures['resultSet'];
            
            $picturesCount = count($pageDetails->pictures);
            if ($picturesCount) {
                $i = 0;
                foreach ( $pageDetails->pictures as $picture ) {
                    
                    $picture->disable_paging = TRUE;
                    $pictureComments = $this->_CI->page_model->get_picture_comments($picture);
                    $pictureLikes = $this->_CI->page_model->get_picture_likes($picture);
                    $pictureSpams = $this->_CI->page_model->spam_details($picture);
                    
                    if ( !empty($picture->thumbnail) ) {
                        $picture->thumbnail = site_url().$this->picture_config['upload_folder'].'/'.$picture->thumbnail;
                    }
                    
                    if ( !empty($picture->fullimage) ) {
                        $picture->fullimage = site_url().$this->picture_config['upload_folder'].'/'.$picture->fullimage;
                    }
                    
                    if ( !empty($picture->intermediate) ) {
                        $picture->intermediate = site_url().$this->picture_config['upload_folder'].'/'.$picture->intermediate;
                    }
                    
                    $pageDetails->pictures[$i]->no_of_comments = count($pictureComments['resultSet']);
                    $pageDetails->pictures[$i]->no_of_likes = count($pictureLikes['resultSet']);
                    $pageDetails->pictures[$i]->is_liked = 0;
                    $pageDetails->pictures[$i]->is_spam = count($pictureSpams['resultSet']) > 0 ? 1 : 0;
                    $pageDetails->pictures[$i]->is_spam_self = 0;
                    
                    if ( count($pictureLikes['resultSet']) ) {
                        foreach ( $pictureLikes['resultSet'] as $pictureLikeIteration ) {
                            if ( $p_data->user_id == $pictureLikeIteration->created_by ) {
                                $pageDetails->pictures[$i]->is_liked = 1;
                                $pageDetails->pictures[$i]->like_id = (int) $pictureLikeIteration->id;
                                break;
                            }
                        }
                    }
                    
                    if ( count($pictureSpams['resultSet']) ) {
                        foreach ( $pictureSpams['resultSet'] as $pictureSpamIteration ) {
                            if ( $p_data->user_id == $pictureSpamIteration->reported_by ) {
                                $pageDetails->pictures[$i]->is_spam_self = 1;
                                break;
                            }
                        }
                    }
                    
                    $i++;
                }
            }
            
            $pageDetails->no_of_pictures = $picturesCount;
        } else {
            return FALSE;
        }
        
        return $pageDetails;
    }
    
    public function get_user_picture_details($p_data=null) {
        
        $this->_CI->load->helper('url');
               
        $pagePictures = $this->_CI->page_model->get_page_images_by_user($p_data);
        $pageDetails->pictures = (array) $pagePictures['resultSet'];
        $pageDetails->more = $pagePictures['more'];
        
        $picturesCount = count($pageDetails->pictures);
        if ($picturesCount) {
            $i = 0;
            foreach ( $pageDetails->pictures as $picture ) {
                
                $picture->disable_paging = TRUE;
                $pictureComments = $this->_CI->page_model->get_picture_comments($picture);
                $pictureLikes = $this->_CI->page_model->get_picture_likes($picture);
                
                if ( !empty($picture->thumbnail) ) {
                    $picture->thumbnail = site_url().$this->picture_config['upload_folder'].'/'.$picture->thumbnail;
                }
                
                if ( !empty($picture->fullimage) ) {
                    $picture->fullimage = site_url().$this->picture_config['upload_folder'].'/'.$picture->fullimage;
                }
                
                if ( !empty($picture->intermediate) ) {
                    $picture->intermediate = site_url().$this->picture_config['upload_folder'].'/'.$picture->intermediate;
                }
                
                unset($picture->created_by);
                
                $pageDetails->pictures[$i]->no_of_comments = count($pictureComments['resultSet']);
                $pageDetails->pictures[$i]->no_of_likes = count($pictureLikes['resultSet']);
            
                $i++;
            }
        }
        
        $pageDetails->no_of_pictures = $picturesCount;
        
        
        return $pageDetails;
    }
    
    public function upload_picture($p_data=null) {
        
        $picture_id = $this->_CI->page_model->upload_picture($p_data);
    
        if ( $picture_id ) {
            
            $ext = $p_data->uploaded_file_data['file_ext'];
            $source = $p_data->uploaded_file_data['file_path'].DIRECTORY_SEPARATOR.$p_data->uploaded_file_data['raw_name'].$ext;
            
            $destination_filename = $this->picture_config['name_prefix_fullimage'].'_'.$picture_id.$ext;
            $destination = $p_data->uploaded_file_data['file_path'].DIRECTORY_SEPARATOR.$destination_filename;
            
            rename($source,$destination);
            
            $config['image_library']    = 'gd2';
            $config['source_image']	    = $destination;
            $config['create_thumb']     = TRUE;
            $config['thumb_marker']     = $this->picture_config['name_suffix_thumbnail'];
            $config['maintain_ratio']   = $this->picture_config['maintain_ratio'];
            $config['width']	        = $this->picture_config['thumbnail_width'];
            $config['height']	        = $this->picture_config['thumbnail_height'];
            
            $config2['image_library']       = 'gd2';
            $config2['source_image']	    = $destination;
            $config2['create_thumb']        = TRUE;
            $config2['thumb_marker']        = $this->picture_config['name_suffix_intermediate'];
            $config2['maintain_ratio']      = $this->picture_config['maintain_ratio'];
            $config2['width']	            = $this->picture_config['intermediate_width'];
            $config2['height']	            = $this->picture_config['intermediate_height'];
            
            $this->_CI->load->library('image_lib'); 
            
            $this->_CI->image_lib->initialize($config);
            $this->_CI->image_lib->resize();
            $this->_CI->image_lib->initialize($config2);
            $this->_CI->image_lib->resize();
            
            $p_data->thumbnail = $this->picture_config['name_prefix_fullimage'].'_'.$picture_id.$this->picture_config['name_suffix_thumbnail'].$ext;
            $p_data->intermediate = $this->picture_config['name_prefix_fullimage'].'_'.$picture_id.$this->picture_config['name_suffix_intermediate'].$ext;
            $p_data->fullimage = $destination_filename;
            $p_data->picture_id = $picture_id;
            
            $this->_CI->page_model->move_and_update_picture($p_data);
            
            return array('picture_id'=>$picture_id);
        } else {
            return FALSE;
        }
    }
    
    public function create_new_page($p_data=null) {
        
        $this->_CI->load->model('user_model');
        
        $academic_data = $this->_CI->user_model->get_active_academic_year();
        
        if ( is_object($academic_data) ) {
            $p_data->academic_id = $academic_data->academic_id;
        } else {
            $p_data->academic_id = null;
        }
        
        $page_status = $this->_CI->page_model->check_page_already_exists($p_data);
        
        if ( !$page_status) {
            return $this->_CI->page_model->create_page($p_data);
        } else {
            return 'PAGE_ALREADY_EXIST';    
        }
    }
    
    public function post_spam($p_data=null) {
        
        $this->_CI->load->model('user_model');
        
        return $this->_CI->page_model->post_spam($p_data);
    }
    
    public function get_hallway_details($p_data=null) {
        
        $this->_CI->load->helper('url');
        
        $pageDetails = $this->_CI->page_model->get_school_picture_details($p_data);
        
        $hallwayDetails = new stdClass();
        
        if ($pageDetails) {
            
            $page_ids = array();
            
            if ( isset($pageDetails['resultSet']) && count($pageDetails['resultSet']) > 0 ) {
                
                foreach ( $pageDetails['resultSet'] as $pageDetailIndividual ) {
                    array_push($page_ids,$pageDetailIndividual->id);
                }
            }
            
            $this->_CI->load->model('school_model');
            $school_users_resultset = $this->_CI->school_model->get_users_by_school($p_data->school_id);
            
            if ( $school_users_resultset == FALSE ) {
                return $school_users_resultset;
            } else {
                $school_users = $school_users_resultset['resultSet'];
            }
            
            $pagePictures = $this->_CI->page_model->get_page_images($page_ids,$school_users);
            
            $hallwayDetails->pictures = (array) $pagePictures['resultSet'];
            
            $picturesCount = count($hallwayDetails->pictures);
            if ($picturesCount) {
                $i = 0;
                foreach ( $hallwayDetails->pictures as $picture ) {
                    
                    $picture->disable_paging = TRUE;
                    $pictureComments = $this->_CI->page_model->get_picture_comments($picture);
                    $pictureLikes = $this->_CI->page_model->get_picture_likes($picture);
                    $pictureSpams = $this->_CI->page_model->spam_details($picture);
                    
                    if ( !empty($picture->thumbnail) ) {
                        $picture->thumbnail = site_url().$this->picture_config['upload_folder'].'/'.$picture->thumbnail;
                    }
                    
                    if ( !empty($picture->fullimage) ) {
                        $picture->fullimage = site_url().$this->picture_config['upload_folder'].'/'.$picture->fullimage;
                    }
                    
                    if ( !empty($picture->intermediate) ) {
                        $picture->intermediate = site_url().$this->picture_config['upload_folder'].'/'.$picture->intermediate;
                    }
                    
                    $hallwayDetails->pictures[$i]->no_of_comments = count($pictureComments['resultSet']);
                    $hallwayDetails->pictures[$i]->no_of_likes = count($pictureLikes['resultSet']);
                    $hallwayDetails->pictures[$i]->is_liked = 0;
                    $hallwayDetails->pictures[$i]->is_spam = count($pictureSpams['resultSet']) > 0 ? 1 : 0;
                    $hallwayDetails->pictures[$i]->is_spam_self = 0;
                    
                    if ( $p_data->show_feeds == TRUE ) {
                        $hallwayDetails->pictures[$i]->feeds = array();
                    }
                    
                    if ( count($pictureComments['resultSet']) ) {
                        foreach ( $pictureComments['resultSet'] as $pictureCommentIteration ) {
                            if ( $p_data->show_feeds == TRUE ) {
                                $hallwayDetails->pictures[$i]->feeds[] = ucfirst($pictureCommentIteration->comment_creator_name).' comment on this image';
                            }
                        }
                    }
                    
                    if ( count($pictureLikes['resultSet']) ) {
                        foreach ( $pictureLikes['resultSet'] as $pictureLikeIteration ) {
                            if ( $p_data->show_feeds == TRUE ) {
                                if ( $pictureLikeIteration->type == 'like' ) {
                                    $hallwayDetails->pictures[$i]->feeds[] = 'Picture liked by '.ucfirst($pictureLikeIteration->like_creator_name);
                                } else if ( $pictureLikeIteration->type == 'dislike' ) {
                                    $hallwayDetails->pictures[$i]->feeds[] = 'Picture unliked by '.ucfirst($pictureLikeIteration->like_creator_name);
                                }
                            }
                            
                            if ( $p_data->user_id == $pictureLikeIteration->created_by ) {
                                $hallwayDetails->pictures[$i]->is_liked = 1;
                                $hallwayDetails->pictures[$i]->like_id = (int) $pictureLikeIteration->id;
                                break;
                            }
                        }
                    }
                    
                    if ( count($pictureSpams['resultSet']) ) {
                        foreach ( $pictureSpams['resultSet'] as $pictureSpamIteration ) {
                            if ( $p_data->show_feeds == TRUE ) {
                                $hallwayDetails->pictures[$i]->feeds[] = 'Picture spammed by '.ucfirst($pictureSpamIteration->spam_reporter_name);
                            }
                            
                            if ( $p_data->user_id == $pictureSpamIteration->reported_by ) {
                                $hallwayDetails->pictures[$i]->is_spam_self = 1;
                                break;
                            }
                        }
                    }
                    
                    $i++;
                }
            }
            
            $hallwayDetails->no_of_pictures = $picturesCount;
            
        } else {
            return FALSE;
        }
        
        return $hallwayDetails;
    }
    
    public function get_images_by_page($p_data=null) {
        $this->_CI->load->helper('url');
               
        $pagePictures = $this->_CI->page_model->get_images_by_page($p_data);
        $pageDetails->pictures = (array) $pagePictures['resultSet'];
        
        $picturesCount = count($pageDetails->pictures);
        if ($picturesCount) {
            foreach ( $pageDetails->pictures as $picture ) {
                if ( !empty($picture->thumbnail) ) {
                    $picture->thumbnail = site_url().$this->picture_config['upload_folder'].DIRECTORY_SEPARATOR.$picture->thumbnail;
                }
                
                if ( !empty($picture->fullimage) ) {
                    $picture->fullimage = site_url().$this->picture_config['upload_folder'].DIRECTORY_SEPARATOR.$picture->fullimage;
                }
            }
        }
        
        return $pageDetails;
    }
}